package com.android.hedgehog.puzzles;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.Assets;
import com.android.hedgehog.Settings;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;

public class MathScreen extends PuzzleScreen {
	enum GameState {
		Ready, Running, Finish
	}

	Game game;
	Graphics graphics;
	GameState state;
	MathField equation;
	int jStep;
	String equationString;
	double resultTime = 0.0;
	int difficulty;
	double readyTime = 0.0;
	String expression;

	public MathScreen(Game game, int gameType, int difficulty, int instNumber) {
		this.game = game;
		this.gameType = gameType;
		graphics = game.getGraphics();
		if (instNumber == 1)
			state = GameState.Ready;
		else
			state = GameState.Running;
		this.difficulty = difficulty;
		equation = new MathField(difficulty);
	}

	@Override
	public void update(List<TouchEvent> touchEvents, double deltaTime) {
		if (state == GameState.Ready) {
			readyTime += deltaTime;
			if (readyTime > 1)
				state = GameState.Running;
		}

		if (state == GameState.Running && equation.selected.size() == 3) {
			success = equation.checkAnswer();
			if (success)
				instanceScore = 10 + difficulty;
			state = GameState.Finish;
		}
		if (state == GameState.Finish) {
			resultTime += deltaTime;
			if (success) {
				if (resultTime > 1.3)
					instanceOver = true;
			} else {
				if (resultTime > 4.6) // test
					instanceOver = true;
			}
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);

			if (state == GameState.Running && event.type == TouchEvent.TOUCH_UP) {
				if (event.x < 64 && event.y > 352 && event.y < 416) {// option0
					equation.selected(0);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x > 64 && event.x < 128 && event.y > 352 && event.y < 416) {// option1
					equation.selected(1);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x > 192 && event.x < 256 && event.y > 352 && event.y < 416) {// option2
					equation.selected(2);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x > 256 && event.y > 352 && event.y < 416) {// option3
					equation.selected(3);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x < 64 && event.y > 416) {// option4
					equation.selected(4);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x > 64 && event.x < 128 && event.y > 416) {// option5
					equation.selected(5);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x > 192 && event.x < 256 && event.y > 416) {// option6
					equation.selected(6);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
				if (event.x > 256 && event.y > 416) {// option7
					equation.selected(7);
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
			}// READYSTATE
		}
	}

	@Override
	public void present() {
		graphics.drawPixmap(Assets.whitebackground, 0, 31);
		graphics.drawText(""+equation.difficulty, 160, 80, 15, Color.BLACK); //test

		if (state == GameState.Ready) {
			graphics.drawSpecialText("SOLVE", 160, 240, 54, 2, Color.BLUE);
		}

		if (state != GameState.Ready) {
			graphics.drawText("" + presentExpression(), 160, 240, 37, Color.BLACK);

			for (int i = 0; i < 2; i++) {// DISPLAY BOXES
				for (int j = 0; j < 4; j++) {
					jStep = j;
					if (j > 1)
						jStep = j + 1;
					if (equation.choice[j + (i * 4)].on == true) {
						graphics.drawRect(0 + (jStep * 64), 352 + (i * 64), 64, 64, Color.GREEN);
					}
					graphics.drawPixmap(Assets.buttons64, 0 + (jStep * 64), 352 + (i * 64));
					graphics.drawSpecialText("" + equation.choice[j + (i * 4)].value, 32 + (jStep * 64),
							395 + (i * 64), 32, 2, Color.BLACK);
				}
			}// DISPLAY BOXES

		}

		if (state == GameState.Finish) {
			if (success) {
				graphics.drawSpecialText("Correct!", 160, 130, 42, 2, Color.GREEN);
				if (gameType == 1) {
					graphics.drawSpecialText("+ " + (instanceScore), 160, 180, 42, 2, Color.GREEN);
				}
			} else {
				graphics.drawSpecialText("WRONG", 160, 130, 42, 2, Color.RED);
				graphics.drawText(equation.correctMistakeLine, 160, 190, 16, Color.GREEN);
			}

		}

	}

	private String presentExpression() {
		expression = "";

		if (equation.selected.size() > 0) {
			expression += equation.selected.get(0).value;
		} else {
			expression += "?";
		}

		expression += " " + equation.operator1.chr + " ";

		if (equation.selected.size() > 1) {
			expression += equation.selected.get(1).value;
		} else {
			expression += "?";
		}

		expression += " " + equation.operator2.chr + " ";

		if (equation.selected.size() > 2) {
			expression += equation.selected.get(2).value;
		} else {
			expression += "?";
		}

		expression += " = " + equation.answer;
		
		return expression;

	}

}
